using System.Collections.Generic;
using BasicScript.Component;
using Game.Main.Model;
using Game.XH;
using UnityEngine;

namespace Game.Component
{
	public class BodyScannerDisplayerItem : BaseItem
	{
		private enum Anim
		{
			Static = 0,
			Game1 = 1,
			Game2 = 2
		}

		private Transform game1;

		private Transform game2;

		private Anim anim;

		private SpriteRenderer waterSprite;

		private SpriteRenderer pill;

		private Transform pillEfc;

		private Transform waterLine;

		private float fillAmount;

		private float pillAlpha;

		private float pillRotate;

		private float pillScale;

		private float game1Timer;

		private int game1AnimStage;

		private List<MoveAnimAObject> animObjs = new List<MoveAnimAObject>();

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			Transform transform = model.Find("Displayer");
			game1 = transform.Find("Game1");
			game1.SetLocalZ(-1E-05f);
			game2 = transform.Find("Game2");
			game2.SetLocalZ(-1E-05f);
			waterSprite = game1.Find("WaterFill").GetComponent<SpriteRenderer>();
			float num = waterSprite.sprite.texture.height;
			Rect textureRect = waterSprite.sprite.textureRect;
			float num2 = textureRect.yMin / num;
			float value = textureRect.yMax / num - num2;
			waterSprite.material.SetFloat("_StartY", num2);
			waterSprite.material.SetFloat("_TotalY", value);
			waterLine = game1.Find("WaterLine");
			pill = game1.Find("Pill").GetComponent<SpriteRenderer>();
			pillEfc = game1.Find("PillEfc");
		}

		protected override void LateUpdate()
		{
			if (anim != Anim.Game1)
			{
				return;
			}
			game1Timer += Time.deltaTime;
			if (game1AnimStage == 0)
			{
				pillAlpha += Time.deltaTime;
				SetPillAlpha(pillAlpha);
				if (pillAlpha >= 1f)
				{
					game1AnimStage = 1;
					game1Timer = 0f;
				}
			}
			else if (game1AnimStage == 1)
			{
				SetPillRotate(pillRotate + Time.deltaTime * 360f);
				SetPillScale(pillScale - Time.deltaTime / 4f);
				SetPillAlpha(pillAlpha - Time.deltaTime / 4f);
				SetFillAmount(fillAmount - Time.deltaTime / 8f);
				if (pillScale <= 0f)
				{
					game1AnimStage = 2;
					game1Timer = 0f;
					SetEffectLoop(pillEfc, false);
				}
			}
		}

		public void SetDisplayerType(int type)
		{
			ResetGame2Anim();
			switch (type)
			{
			case 0:
				game1.gameObject.SetActive(false);
				game2.gameObject.SetActive(false);
				break;
			case 1:
				game1.gameObject.SetActive(true);
				game2.gameObject.SetActive(false);
				ResetGame1Anim();
				break;
			case 2:
				game1.gameObject.SetActive(false);
				game2.gameObject.SetActive(true);
				break;
			}
		}

		public void ResetGame1Anim()
		{
			SetFillAmount(1f);
			SetPillAlpha(0f);
			game1Timer = 0f;
			anim = Anim.Static;
			pillEfc.gameObject.SetActive(false);
			SetEffectLoop(pillEfc, false);
		}

		public void PlayGame1Anim(SpriteRenderer spriteRender)
		{
			SetFillAmount(1f);
			SetPillAlpha(0f);
			SetPillRotate(0f);
			SetPillScale(1f);
			game1Timer = 0f;
			game1AnimStage = 0;
			anim = Anim.Game1;
			pillEfc.gameObject.SetActive(true);
			SetEffectLoop(pillEfc, true);
			if (spriteRender != null)
			{
				pill.sprite = spriteRender.sprite;
			}
			else
			{
				pill.sprite = null;
			}
		}

		public void ResetGame2Anim()
		{
			anim = Anim.Static;
			for (int i = 0; i < animObjs.Count; i++)
			{
				if (animObjs[i] != null)
				{
					animObjs[i].DestroySelf();
				}
			}
			animObjs.Clear();
		}

		public void PlayGame2Anim()
		{
			Transform transform = game2.Find("Anim/Copy");
			if (transform != null)
			{
				GameObject gameObject = Object.Instantiate(transform.gameObject);
				gameObject.transform.SetParent(transform.parent);
				MoveAnimAObject moveAnimAObject = gameObject.AddComponent<MoveAnimAObject>();
				List<Vector3> list = new List<Vector3>();
				list.Add(new Vector3(-0.487f, 0.895f));
				list.Add(new Vector3(0.894f, 0.895f));
				list.Add(new Vector3(0.894f, 0.433f));
				list.Add(new Vector3(-0.904f, 0.433f));
				list.Add(new Vector3(-0.904f, 0.012f));
				list.Add(new Vector3(0.867f, 0.012f));
				list.Add(new Vector3(0.867f, -0.45f));
				list.Add(new Vector3(-0.897f, -0.45f));
				list.Add(new Vector3(-0.897f, -0.932f));
				list.Add(new Vector3(0.54f, -0.932f));
				list.Add(new Vector3(0.54f, -1.128f));
				moveAnimAObject.SetPoints(list);
				animObjs.Add(moveAnimAObject);
				moveAnimAObject.gameObject.SetActive(true);
				moveAnimAObject.PlayAnim();
			}
		}

		private void SetFillAmount(float val)
		{
			fillAmount = val;
			waterSprite.material.SetFloat("_Fill", fillAmount);
			waterLine.SetLocalY(1.5f * val - 1.04f);
		}

		private void SetPillAlpha(float val)
		{
			pillAlpha = val;
			pill.color = new Color(1f, 1f, 1f, val);
		}

		private void SetPillRotate(float val)
		{
			pillRotate = val;
			pill.transform.SetLocalRotationZ(val);
		}

		private void SetPillScale(float val)
		{
			pillScale = val;
			pill.transform.SetLocalScaleXY(val, val);
		}
	}
}
